<{extends file="manage/layout.tpl"}>
<{block name='css'}>
<link rel="stylesheet" href="/css/system-user.css" />
<{/block}>
<{block name=title}><{/block}>

<{block name=content}>
<section class="v6-m-mainBox">
	<div class="v6-m-mainContent">
		<div class="row">
			<div class="col-md-12">
				<div class="panel panel-info">
					<div class="panel-heading">
						<ol class="v6-m-breadcrumb">
							<li>用户管理</li>
							<li class="active">后台用户</li>
						</ol>
					</div>
					<div class="panel-body">
						<div class="col-md-12 m-margin-bottom">
							<button type="button" class="btn btn-primary btn-md add-click-btn"  data-toggle="modal" data-target="#myaddModal">添加</button>
						</div>
						<div class="col-md-2">
						<input type="text" id="user_name" name="user_name" class="form-control searchInput" placeholder="用户名">
						</div>
						<div class="col-md-2">
							<input type="text" id="relName" name="real_name" class="form-control searchInput" placeholder="真实姓名">
						</div>
						<div class="col-md-2">
							<input type="text" id="tel" name="phone" class="form-control searchInput" placeholder="联系电话">
						</div>
						<div class="col-md-2">
							<input type="text" id="position" name="position" class="form-control searchInput" placeholder="职位">
						</div>
						<div class="col-md-2">
							<select class="form-control searchInput" name="status">
								<option value="">状态</option>
								<option value="1">正常</option>
								<option value="0">禁用</option>
							</select>
						</div>
						<div class="col-md-2">
							<button type="button" onclick="searchUser();" class="btn btn-primary" >查询</button>
						</div>
					<div class="col-md-12">
							<table class="table table-bordered table-impresive pager-table m-margin-top" data-size="10"  id="admin_user">
								<thead>
								<tr>
									<th>用户名</th>
									<th>角色组</th>
									<th>姓名</th>
									<th>电话</th>
									<th>职位</th>
									<th>状态</th>
									<th>时间</th>
									<th>操作</th>
								</tr>
								</thead>
								<tbody>
								</tbody>
							</table>
							<div class="pager-foot">
								<div class="col-lg-6">
									<div class="dataTables_info">共<span class="pager-total"></span>条数据</div>
								</div>
								<div class="col-lg-6">
									<ul class="pagination pager-page" style="float: right;">
									</ul>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>

		</div>
	</div>
	<!-- 添加系统用户 -->
	<div class="modal fade" id="myaddModal" tabindex="-1" role="dialog" aria-labelledby="myaddModalLabel" style="z-index: 11111111;">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close m-add-sysClose" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="myaddModalLabel">添加系统用户</h4>
				</div>
				<div class="modal-body add-system-info">
					<form class="add_admin_form" method="post">
						<div>
							<label for="add-system-logoname">用户名：</label>
							<input name="id" id="add-sysytem-id" type="hidden" required>
							<input id="add-system-logoname" disabled="disabled" name='user_name' type="text" placeholder="请输入用户名"  required/>
						</div>
						<div>
							<label for="">角色组：</label>
							<select class="form-control select-css select-add-info" name="group_id"></select>
						</div>
						<div>
							<label for="add-system-name">姓名：</label>
							<input id="add-system-name" name='real_name' type="text" placeholder="请输入真实姓名" required/>
						</div>
						<div>
							<label for="add-system-tel">电话：</label>
							<input id="add-system-tel" name='phone' type="tel" placeholder="请输入电话" required />
						</div>
						<div>
							<label for="add-system-position">职位：</label>
							<input id="add-system-position" name='position' type="text" placeholder="请输入职位" required />
						</div>
						<div class="form-submit">
							<button class="btn btn-md btn-info add_admin_submit">点击提交</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</section>
<template id="item">
	<tr>
		<td>{user_name}</td>
		<td>{group_name}</td>
		<td>{real_name}</td>
		<td>{phone}</td>
		<td>{position}</td>
		<td>{status}</td>
		<td>{create_time}</td>
		<td><button  class="btn btn-xs btn-info m-edit-btn" data-toggle="modal" data-target="#myaddModal" data-id="{id}">修改</button>
		</td>
	</tr>
</template>
<{/block}>

<{block name=js}>
<script type="text/javascript" src="/js/pager/ajaxPage.js"></script>
<script type="text/javascript" src="/js/valid.min.js"></script>
<script type="text/javascript">
	var adminTable = '';
	$(function(){
		Pager.init(function(){
			adminTable = Pager.getObj('admin_user');
			adminTable.url = '/manage/muser/get_muser_list';
			adminTable.fun = function(data){
				$.each(data, function(index, data){
					data.status = data.status ? "<span class='label label-success'>正常</span>" : "<span class='label label-danger'>禁用</span>";
					data.create_time = getLocalTime(data.create_time);
				})
				return data;
			}
		})
	})

	$(".add_admin_submit").click(function(){
		var login=$("#add-system-logoname").val();
		if(login==''||login==null){
			layer.msg("登录名不能为空");
			return false;
		}
		var name=$("#add-system-name").val();
		if(name==''||name==null){
			layer.msg("真实姓名不能为空");
			return false;
		}
		var tel=$("#add-system-tel").val();
		if(tel==''||tel==null){
			layer.msg("联系电话不能为空");
			return false;
		}
		var position=$("#add-system-position").val();
		if(position==''||position==null){
			layer.msg("职位不能为空");
			return false;
		}
		if(login!=''&&login!=null){
			var addid=$("#add-sysytem-id").val();
			if(addid!=''){
				var data = $('.add_admin_form').serialize();
				ajaxObj('/manage/muser/addedit_muser', 'post', data, function(obj){
					if( obj.code == 1000){
						layer.msg("修改成功");
						$('#myaddModal').modal('hide');
						resetFrom($('.add_admin_form')[0]);
						adminTable.reload(true);
					}
				},errorFn)
			}else{
				ajaxObj('/manage/muser/is_login_name', 'post', {user_name:login}, function(obj){
					if( obj.code == 1000){
						if(obj.data=='0'){
							layer.msg("登录名已存在");
							return;
						}else{
							var data = $('.add_admin_form').serialize() + "&status=1";
							ajaxObj('/manage/muser/addedit_muser', 'post', data, function(obj){
								if( obj.code == 1000){
									layer.msg("添加成功");
									$('#myaddModal').modal('hide');
									resetFrom($('.add_admin_form')[0]);
									adminTable.reload(true);
								}
							},errorFn)
						}
					}
				},errorFn)
			}
		}
		return false;
	})
	
	
	$('.add-click-btn').click(function(){
		$("#add-system-logoname").removeAttr("disabled");
		$("#add-sysytem-id").val('');
		get_group_info('');//获取角色信息
		resetFrom($('.add_admin_form')[0]);
	})

	$('#admin_user').delegate('.m-edit-btn', 'click', function(){
		$("#add-system-logoname").attr("disabled","disabled");
		$('#myaddModalLabel').html('修改系统用户');
		var data = adminTable.getRow('id', $(this).data('id'));
		$("#add-sysytem-id").val(data.id);
		$("#add-system-logoname").val(data.user_name);
		$("#add-system-name").val(data.real_name);
		$("#add-system-tel").val(data.phone);
		$("#add-system-position").val(data.position);
		get_group_info(data.group_id);//获取角色信息
	})

	function get_group_info(group){
		ajaxObj('/manage/muser/get_group', 'post', {}, function(obj){
			if(obj.code == 1000){
				$('.select-add-info').html('');
				if(obj.data.length){
					var str = '';
					$.each(obj.data, function(index, data){
						if(data.id == group){
							str += '<option selected="selected" value="'+ data.id +'">'+ data.group_name +'</option>';
						}else{
							str += '<option value="'+ data.id +'">'+ data.group_name +'</option>';
						}
					})
					$('.select-add-info').html(str);
				}else{
					$('.select-add-info').html('<option value="">暂无村镇信息</option>')
				}
			}
		})
	}
	
	function searchUser(){
		$.each($('.searchInput'), function(index, obj){
			adminTable.addParam($(obj).attr("name"), $(obj).val());
		})
		adminTable.reload(true);
	}


	function resetFrom( targ ){
		targ.reset();
	}
</script>

<{/block}>
